package com.lz.football.module.techBar.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.TechBarEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	import com.liangzhong.model.type.GoodsType;
	import com.liangzhong.model.type.MaterialType;
	import com.liangzhong.model.type.OtherGoodType;
	import com.lz.football.module.techBar.item.TechGoodsItem;
	
	import common.base.GoodsUsePanel;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.events.ListEvent;
	import lz.controls.managers.PopUpManager;
	import lz.controls.managers.TipsManager;

	/**
	 *角色洗练 
	 * @author lake
	 * //黄金十二宫未点亮全部球星，不能进行金色强化！
	 */	
	public class RoleRefreshPanel extends TechBasePanel implements ITechPanel
	{
		/**
		 *洗练卡容器皮肤 guide 
		 */		
		public static var 					freshCardSkin :DisplayObject;
		/**
		 *成功率 
		 */		
		private var txtRate				:TextField;
		/**
		 *球员卡容器 
		 */		
		private var playerCardContainer	:MovieClip;
		/**
		 *球员卡容器 
		 */		
		private var playerCardContainer1	:MovieClip;
		/**
		 *开始
		 */		
		public static var startBtn			:SimpleButton;
		/**
		 *规则 
		 */		
		private var ruleBtn				:SimpleButton;
		/**
		 *洗练卡位置 
		 */		
		private var freshCarditem			:TechGoodsItem;
		/**
		 *球员卡 
		 */		
		private var playerCard 			:*;
		/**
		 *球员卡 
		 */		
		private var playerCard1 			:*;
		/**
		 *自己角色 
		 */		
		private var selfVo 			:PlayerStarVo;
		/**
		 *成功概率
		 * 1 - 8 
		 */		
		private var sucessRates		:Array = [100,100,31,23,19,17,13,9,5,1];
		/**
		 *需要使用的item
		 * 物品位置 
		 */		
		private var useItemList		:Array = [];
		/**
		 *选中的幸运卡位 
		 */		
		private var selectLuckItem		:TechGoodsItem;
		/**
		 *幸运卡列表 
		 */		
		private var luckyGoodsList		:Array = [];
		/**
		 *总成功率 
		 */		
		private var totalRate			:int = 0;
		/**
		 *使用幸运卡数 
		 */		
		private var _useLuckyNum		:int = 0;
		
		public function RoleRefreshPanel()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		override public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.techbar.view.RoleRefreshSkin");
			this.addChild(_symbolSkin);
			startBtn = _symbolSkin.startBtn;
			ruleBtn = _symbolSkin.ruleBtn;
			txtRate = _symbolSkin.txtRate;
			txtRate.text = "";
			playerCardContainer = _symbolSkin.playerCardContainer;
			playerCardContainer1 = _symbolSkin.playerCardContainer1;
			//球员卡左
			playerCard = Reflection.createInstance("com.liangzhong.view.tips.PlayerCardTips");
			selfVo = AppData.getInstance().playerCenterDataManager.getSelfPlayerStar();
			playerCard.cdata = selfVo;
			playerCardContainer.addChild(playerCard);
			//球员卡右
			playerCard1 = Reflection.createInstance("com.liangzhong.view.tips.PlayerCardTips");
			var plStar :PlayerStarVo = AppData.getInstance().playerCenterDataManager.getSelfPlayerStar();
			playerCard1.cdata = plStar;
			playerCardContainer1.addChild(playerCard1);
			//洗练卡位置
			freshCarditem = new TechGoodsItem();
			freshCarditem.initUI(_symbolSkin.item_3);
			//幸运卡位置
			var i :int = 0;
			var tItem :TechGoodsItem;
			for(;i<3;i++)
			{
				tItem = new TechGoodsItem();
				tItem.initUI(_symbolSkin["item_" + i]);
				useItemList.push(tItem);
				tItem.addEventListener(ListEvent.ITEM_CLICK,useItemClickHandler)
			}
			//事件
			addEvtListener();
			//@lake 2013-11-16
			freshCardSkin = _symbolSkin.item_3;
			//效果
			effectMc = _symbolSkin.effectMc;
			effectMc.gotoAndStop(1);
			effectMc.visible = false;
			//特效球员信息
			if(effectMc)
			{
				effectMc.pmc.mc.txtName.text = "" + selfVo.name;
				//大头像
				var bigImgBg :MovieClip = effectMc.pmc.mc.bigImgBg;
				var bigBData :BitmapData = selfVo.bigBitmapData;
				var bigBitMap :Bitmap = new Bitmap(bigBData);
				DisplayObjectUtil.removeAllChildren(bigImgBg);
				if(bigImgBg)
				{
					bigImgBg.addChild(bigBitMap);
				}
			}
		}
		/**
		 *注册事件 
		 * 
		 */	
		override public function addEvtListener():void
		{
			freshCarditem.addEventListener(ListEvent.ITEM_CLICK,freshItemClickHandler);
			startBtn.addEventListener(MouseEvent.CLICK,startClickHandler);
			ruleBtn.addEventListener(MouseEvent.CLICK,ruleClickHandler);
		}
		/**
		 *幸运卡使用位置点击 
		 * @param e
		 */		
		private function useItemClickHandler(e :ListEvent):void
		{
			selectLuckItem = e.currentTarget as TechGoodsItem;
			if(selectLuckItem.data)
			{
				luckyGoodsList.push(selectLuckItem.data);
				useLuckyNum -- ;
				selectLuckItem.data = null;
				TipsManager.hide();
				return ;
			}
			var goodsUsePanel :GoodsUsePanel = GoodsUsePanel.inistance;
			goodsUsePanel.dragEnabled = true;
			goodsUsePanel.addEvtListener();
			goodsUsePanel.goodsList = luckyGoodsList;
			goodsUsePanel.showBtn(3);
			goodsUsePanel.callBack = luckyItemUse;
			var globPoint :Point = this.localToGlobal(new Point(0,0));
			PopUpManager.addWindow(goodsUsePanel,GlobalDef.GameWindowContainer,globPoint.x - 135,globPoint.y,true);
		}
		/**
		 *幸运卡使用 
		 * 
		 */		
		private function luckyItemUse(goodsVo :GoodsVo):void
		{
			selectLuckItem.data = goodsVo;
			useLuckyNum ++ ;
			GoodsUsePanel.inistance.removeGoods(goodsVo);
			GoodsUsePanel.inistance.rmoveStage();
		}
		/**
		 *幸运数改变 
		 * @param value
		 * 
		 */		
		public function set useLuckyNum(value :int):void
		{
			_useLuckyNum = value;
			/*var showRate:int = totalRate * (1 + 0.1 * _useLuckyNum);
			if(showRate > 100)
			{
				showRate = 100;
			}
			txtRate.text = showRate +  "%";*/
		}
		public function get useLuckyNum():int
		{
			return _useLuckyNum;
		}
		/**
		 *规则点击 
		 * @param e
		 * 
		 */		
		override protected function ruleClickHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new TechBarEvent("rule_Click",7,true));
		}
		/**
		 *点击洗练卡 
		 * @param e
		 * 
		 */		
		private function freshItemClickHandler(e :ListEvent):void
		{
			if(freshCarditem.data)
			{
				txtRate.text = "";
				freshCarditem.data = null;
				TipsManager.hide();
				return ;
			}
			var goodsUsePanel :GoodsUsePanel = GoodsUsePanel.inistance;
			goodsUsePanel.dragEnabled = true;
			goodsUsePanel.addEvtListener();
			goodsUsePanel.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.MATERIAL,MaterialType.FRESH_CARD);
			goodsUsePanel.showBtn(3);
			goodsUsePanel.callBack = freshItemUse;
			var globPoint :Point = this.localToGlobal(new Point(0,0));
			PopUpManager.addWindow(goodsUsePanel,GlobalDef.GameWindowContainer,globPoint.x - 135,globPoint.y,true);
			//guide
			AppEventDispatcher.getInstance().dispatchEvt("Guide_RoleFresh_Item_C");
		}
		/**
		 *洗练卡使用 
		 * @param goodsVo
		 * 
		 */		
		private function freshItemUse(goodsVo :GoodsVo):void
		{
			if(selfVo.color > goodsVo.color)
			{
				Alert.showErrorTip("强化卡品质不能低于球员品质");
				return;
			}
			var gactive :int = AppData.getInstance().goldPalaceManager.goldPalaceVo.active;
			if(goodsVo.color > 7 && gactive < 12)
			{
				Alert.showErrorTip("还未开启金色强化，请查看黄金十二宫!");
				return;
			}
			var roleColor 	:int = selfVo.color;
			var freshColor 	:int = goodsVo.color;
			var baseRate	:int = sucessRates[roleColor];//基础概率
			var addRate		:int = 0;//额外概率
			for(var i :int = roleColor + 1;i < freshColor;i++)
			{
				addRate += sucessRates[i];
			}
			totalRate = int(baseRate + addRate/2);
			var showRate:int = totalRate * (1 + 0.1 * _useLuckyNum);
			if(showRate > 100)
			{
				showRate = 100;
			}
			txtRate.text = showRate +  "%";
			freshCarditem.data = goodsVo;
			GoodsUsePanel.inistance.rmoveStage();
		}
		/**
		 *点击左边物品 
		 * @param e
		 * 
		 */		
		private function showItemClickHandler(e :ListEvent):void
		{
			var goodsVo :GoodsVo = e.item as GoodsVo;
			if(goodsVo == null)
			{
				return;
			}
			playerCard.setPureData(goodsVo,playerCard.getChildAt(0));
		}
		/**
		 *重设 恢复初始化 
		 * 
		 */		
		override public function reset():void
		{
			_useLuckyNum = 0;
			freshCarditem.data = null;
			for each(var tItem :TechGoodsItem in useItemList)
			{
				tItem.data = null;
			}
			startBtn.mouseEnabled = true;
			selfVo = AppData.getInstance().playerCenterDataManager.getSelfPlayerStar();;
			playerCard.setPureData(selfVo,playerCard.getChildAt(0));
			var plStar :PlayerStarVo = AppData.getInstance().playerCenterDataManager.getSelfPlayerStar();
			var gactive :int = AppData.getInstance().goldPalaceManager.goldPalaceVo.active; 
			if((plStar.color < 7)||(plStar.color == 7 && gactive >= 12))
			{
				plStar.color += 1;
			}
			playerCard1.setPureData(plStar,playerCard1.getChildAt(0));
			txtRate.text = "";
			luckyGoodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.FUCKLYCARD);
			effectMc.visible = false;
			effectMc.gotoAndStop(1);
		}
		/**
		 *开始
		 * @param e
		 * 
		 */		
		private function startClickHandler(e :MouseEvent):void
		{
			/*if(selfVo.color >= 7)
			{
				Alert.showErrorTip("已达顶级");
				return ;
			}*/
			if(freshCarditem.data == null)
			{
				Alert.showErrorTip("未选择强化卡");
				return;
			}
			var id1 :int = (freshCarditem.data as GoodsVo).pid;
			var ids :Array = [];
			var tItem :TechGoodsItem;
			for(var i :int = 0;i<3;i++)
			{
				tItem = useItemList[i];
				if(tItem.data)
				{
					ids.unshift(tItem.data.pid);
				}
				else
				{
					ids.push(0);
				}
			}
			this.dispatchEvent(new TechBarEvent(TechBarEvent.ROLE_REFRESH,{id1:id1,id2:ids[0],id3:ids[1],id4:ids[2]},true));
			startBtn.mouseEnabled = false;
		}
		/**
		 *删除事件 
		 * 
		 */	
		override public function removeEvtListener():void
		{
			startBtn.removeEventListener(MouseEvent.CLICK,startClickHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		override public function destroyView():void
		{
			removeEvtListener();
			for each(var tItem :TechGoodsItem in useItemList)
			{
				tItem.destroyView();
				tItem.removeEventListener(ListEvent.ITEM_CLICK,useItemClickHandler);
			}
			luckyGoodsList = null;
			freshCardSkin = null;
			startBtn = null;
			TweenLite.killTweensOf(reset);
			TweenLite.killTweensOf(showAlertRes);
			DisplayObjectUtil.removeAllChildren(this);
			_symbolSkin = null;
		}
		/**
		 *角色强化
		 * 
		 */		
		public function roleRefresh(value :Object):void
		{
			if(value.retcode == 0)
			{
				TweenLite.delayedCall(3,showAlertRes,[value.color]);
				if(effectMc)
				{
					effectMc.visible = true;
					effectMc.gotoAndPlay(1);
				}
				TweenLite.delayedCall(3,reset);
			}
			else
			{
				reset();
			}
		}
		/**
		 *显示成功失败 
		 * @param value
		 * 
		 */		
		private function showAlertRes(value :int ):void
		{
			if(value == 0)
			{
				Alert.showErrorTip("强化失败");
			}
			else
			{
				Alert.showErrorTip("强化成功");
			}
		}
	}
}